﻿@using Microsoft.AspNetCore.Components.Web
@using Microsoft.Extensions.Localization
@using Project.Constraints.Models
@using Project.Constraints.Utils
<Form Model="LoginModel" Method="@HttpMethod.Post" OnFinish="DoLogin" Name="login">
    <FormItem>
        <Input @bind-Value="@LoginModel.UserName" Placeholder="@Localizer["Login.Placeholder.UserName"].Value" Type="@InputType.Text" tabindex="1">
        <Prefix>
            <Icon Type="user" Theme="IconThemeType.Outline" />
        </Prefix>
        </Input>
    </FormItem>
    <FormItem>
        <InputPassword @bind-Value="LoginModel.Password" placeholder="@Localizer["Login.Placeholder.Password"].Value" tabindex="2">
            <Prefix>
                <Icon Type="lock" Theme="IconThemeType.Outline" />
            </Prefix>
        </InputPassword>
    </FormItem>
    <FormItem>
        <button class="ant-btn ant-btn-primary" type="submit">
            @Localizer["Login.Buttons.Login"]
        </button>
    </FormItem>
</Form>

@code {

    [Inject, NotNull] IStringLocalizer? Localizer { get; set; }
    [SupplyParameterFromForm] public LoginFormModel LoginModel { get; set; } = new();
    [Parameter, NotNull] public Func<LoginFormModel, Task>? HandleLogin { get; set; }

    bool loading = false;

    private async Task DoLogin()
    {
        using var _ = BooleanStatusManager.New(b => loading = b);
        // await Task.Yield();
        if (HandleLogin != null)
        {
            await HandleLogin.Invoke(LoginModel);
        }
    }
}
